package com.cskaoyan.interceptor;


import com.cskaoyan.bean.Log;
import com.cskaoyan.bean.User;
import com.cskaoyan.mapper.AdminMapper;
import com.cskaoyan.mapper.LogMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.cskaoyan.service.LogService;
import com.cskaoyan.util.GetLogedAdmin;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 创建日期: 2022/05/06 18:01
 *
 * @author 孙豪骏
 */

//这个是用来增加操作日志的
@Component
public class AdminInterceptor implements HandlerInterceptor {
    @Autowired
    LogService logService;

    //对于每个过滤器的请求都选择放行


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    //对于每个/admin /role 的 create delete update 都进行了些许分类
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        String requestURI = request.getRequestURI();
        String ip = request.getRemoteAddr();
        String[] split = requestURI.split("/");
        String mid = split[split.length - 2];
        String op = split[split.length - 1];
        String action = "未作修改查看页面";
        if ("admin".equals(mid)) {
            switch (op) {
                case "update":
                    action = "编辑了管理员";
                    break;
                case "create":
                    action = "新增了管理员";
                    break;
                case "delete":
                    action = "删除了管理员";
                    break;
            }
        }
        if ("role".equals(mid)) {
            switch (op) {
                case "update":
                    action = "编辑了角色";
                    break;
                case "create":
                    action = "新增了角色";
                    break;
                case "delete":
                    action = "删除了角色";
                    break;
            }
        }
        Log log = new Log();
        if (GetLogedAdmin.getLogedAdmin() != null) {
            log.setAdmin(GetLogedAdmin.getLogedAdmin().getUsername());
        }
        log.setIp(ip);
        log.setAction(action);
        log.setStatus(true);
        log.setType(1);
        log.setDeleted(false);
        if ("auth".equals(mid) && "login".equals(op)) {
            log.setAction("用户登录");
        }

        if ("admin".equals(mid) || "role".equals(mid) || "auth".equals(mid)) {
            logService.insert(log);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}